<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>分析报告编辑</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/static/layui/css/layui.css" media="all">
</head>
<body>

<div class="layui-form" lay-filter="layuiadmin-app-form-list" id="layuiadmin-app-form-list"
     style="padding: 20px 30px 0 0;">
    <div class="layui-row layui-form-item">
        <input type="text" name="id" class="form-control" hidden="hidden">
        <div class="layui-col-xs6">
            <label class="layui-form-label">报告名称：</label>
            <div class="layui-input-block">
                <input type="text" name="name" id = "name" lay-verify="required" autocomplete="off" class="layui-input"
                       placeholder="报告名称">
            </div>
        </div>
        <div class="layui-col-xs6">
            <label class="layui-form-label">备注：</label>
            <div class="layui-input-block">
                <input type="text" name="remark" id = "remark" lay-verify="required" autocomplete="off" class="layui-input"
                       placeholder="备注">
            </div>
        </div>
    </div>
    <div class="layui-row layui-form-item">
        <input hidden="hidden" name="path" id="path">

        <div class="layui-col-xs6">
            <div class="layui-form-item">
                <label class="layui-form-label">上传报告</label>
                <div class="layui-input-block">
                    <button type="button" class="layui-btn" id="uploadFile" lay-verify="isupload">选择报告</button>
                </div>
            </div>
        </div>
        <div class="layui-col-xs6">
            <div class="layui-form-item">
                <label class="layui-form-label">附件</label>
                <div class="layui-input-block">
                    <span id="fileName" name ="fileName"></span>
                </div>
            </div>
        </div>
        <div class="layui-form-item layui-hide">
            <input type="button" lay-submit lay-filter="layuiadmin-app-form-submit" id="layuiadmin-app-form-submit"
                   value="确认添加">
            <input type="button" lay-submit lay-filter="layuiadmin-app-form-edit" id="layuiadmin-app-form-edit"
                   value="确认编辑">
        </div>
    </div>
    <script src="/static/layui/layui.all.js"></script>
    <script>
        var filePath;
        var fileName;
        var fileSuffix;
        layui.config({
            base: '/static/' //静态资源所在路径
        }).extend({
            //主入口模块
            index: 'lib/index'
        }).use(['index', 'form'], function () {
            var $ = layui.$, form = layui.form, admin = layui.admin;
            var upload = layui.upload;
            var e = layui.$;
            var choose_file_flag = false;
            form.on('switch(status)', function (data) {
                var state = data.elem.checked ? 0 : 1;
                $(data.elem).attr('type', 'hidden').val(state);
            });

            upload.render({
                elem: '#uploadFile', //绑定元素（上传文件的id）
                url: '/bs/file/upload', //上传接口
                method: 'POST',
                auto: true,
                exts: 'pdf|doc|docx', //只允许上传压缩文件
                accept: 'file',//指定允许上传时校验的文件类型
                acceptMime: 'pdf/*,docx/*,doc/*',//规定打开文件选择框时，筛选出的文件类型，值为用逗号隔开的 MIME 类型列表
                field: 'file',//设定文件域的字段名
                //bindAction: '#submit', //指向一个按钮触发上传
                size: 102400,//限制文件大小10M
                multiple: false,
                data: {},
                choose: function (obj) {
                    obj.preview(function (index, file, result) {
                        if (file.name.length > 0) {
                            choose_file_flag = true;
                            $("#fileName").text(file.name);
                            fileName = file.name;
                        }
                    });
                },
                before: function (obj) {
                    obj.preview(function (index, file, result) {
                        layer.load();
                        if (file.name.length > 0) {
                            choose_file_flag = true;
                        }
                    });

                },
                done: function (res, index, upload) {//上传完毕回调
                    if (choose_file_flag) {
                        layer.closeAll('loading');
                        if (res.resultStat == 'SUCCESS') {
                            //上传完成，文件路径进行入库存储
                            var data = res.data;
                            filePath = data.path;
                            fileSuffix = data.fileSuffix;
                            $("#path").val(filePath);
                        } else {
                            // parent.layer.alert(res.info)
                        }
                    }
                }
                , error: function () {//请求异常回调
                    layer.closeAll('loading');
                    layer.alert('系统异常！');
                }
            });

            form.verify({
                isupload: function (value, item) {//isupload的是lay-verify="isupload"定义的
                    if (choose_file_flag == false) {
                        return '请选择报告';
                    }
                }
            });
            $("#layuiadmin-app-form-submit").on("click", function (index) {
                var name = $("#name").val();
                var remark =$("#remark").val();
                var data = {
                   path: filePath,
                   fileName: fileName,
                   fileSuffix: fileSuffix,
                   name: name,
                   remark: remark
                };
                admin.req({
                    type: 'post',
                    url: "/bs/analysisReport/saveOrUpdate",
                    data: data,
                    accept: 'file',
                    cache: false,
                    done: function (res) {
                    }
                });
            });

        });

    </script>
</body>
</html>